﻿Imports extranet.BE
Imports extranet.DA
Imports System.IO
Imports System
Imports extranet

Public Class BCForum

  Public Shared Function ListaSubVisiveis() As List(Of BEForum)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaCatSubcatVisiveis()
  End Function

  Public Shared Function ListaSubVisiveis(ByVal idCategoria As Integer) As List(Of BESubcategoria)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaSubcatVisiveis(idCategoria)
  End Function

  Public Shared Function ListaTodasSub() As List(Of BEForum)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaTodasCatSubcat()
  End Function

  Public Shared Function ListaTodasSub(ByVal idCategoria As Integer) As List(Of BESubcategoria)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaTodasSubcat(idCategoria)
  End Function

  Public Shared Function ListarCategoriasSemRepeticao(ByVal lstobjBEForum As List(Of BEForum)) As List(Of BEForum)
    Dim lista As New List(Of BEForum)
    Dim i As Integer
    For i = 0 To lstobjBEForum.Count - 1
      If i = 0 Then
        lista.Add(lstobjBEForum(i))
      Else
        If (lista(lista.Count - 1).Nome) <> lstobjBEForum(i).Nome Then
          lista.Add(lstobjBEForum(i))
        End If
      End If
    Next
    Return lista
  End Function
  Public Shared Function ListarTodasCategorias() As List(Of BEForum)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionarTodasCategorias()
  End Function

  Public Shared Function ListarCategoriasVisiveis() As List(Of BEForum)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionarCategoriasVisiveis()
  End Function


  Public Shared Function SelecionaCategoria(ByVal idcategoria As String) As BEForum
    Dim objDAForum As New DAForum
    Return objDAForum.BuscaCategoria(idcategoria)
  End Function

  Public Shared Function SelecionaSubcategoria(ByVal idsubcategoria As String) As BESubcategoria
    Dim objDAForum As New DAForum
    Return objDAForum.BuscaSubcategoria(idsubcategoria)
  End Function

  Public Shared Function ListarTopicosVisiveis(ByVal idsubcategoria As String) As List(Of BETopico)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaTopicosVisiveis(idsubcategoria)
  End Function
  Public Shared Function ListarTopicosNaoPendentes(ByVal idsubcategoria As String) As List(Of BETopico)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionaTopicosNaoPendentes(idsubcategoria)
  End Function


  Public Shared Function ListarPosts(ByVal tipo As Integer, ByVal idtopico As String) As List(Of BEPost)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionarPosts(tipo, idtopico)
  End Function

  Public Shared Function ListaPostsPendentes() As List(Of BETopicoPostPendente)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionarPostsPendentes()
  End Function

  Public Shared Function ListaTopicosPendentes() As List(Of BETopicoPostPendente)
    Dim objDAForum As New DAForum
    Return objDAForum.SelecionarTopicosPendentes()
  End Function

  Public Shared Function SelecionaTopico(ByVal idtopico As Integer) As BETopico
    Dim objDAForum As New DAForum
    Return objDAForum.BuscaTopico(idtopico)
  End Function
  Public Shared Function SelecionaPost(ByVal idpost As Integer) As BEPost
    Dim objDAForum As New DAForum
    Return objDAForum.BuscaPost(idpost)
  End Function


  Public Shared Sub AprovaTopicoOuPost(ByVal objBEMensagem As BEMensagem)
    Dim objDAForum As New DAForum
    objDAForum.AprovaTopicoOuPost(objBEMensagem)
  End Sub
  Public Shared Sub ReprovaPostOuTopico(ByVal objBEMensagem As BEMensagem)
    Dim objDAForum As New DAForum
    objDAForum.ExcluiMensagem(objBEMensagem.Id)
  End Sub

  Public Shared Sub AdicionarCategoria(ByVal objBECategoria As BEForum)
    Dim objDAForum As New DAForum
    objDAForum.AddCategoria(objBECategoria)
  End Sub
  Public Shared Sub AdicionarSubcategoria(ByVal objBESubcategoria As BESubcategoria)
    Dim objDAForum As New DAForum
    objDAForum.AddSubcategoria(objBESubcategoria)
  End Sub
  Public Shared Sub AdicionarTopico(ByVal objBETopico As BETopico, ByVal objBEMensagem As BEMensagem)
    Dim objDAForum As New DAForum
    objBETopico.IdMensagem = BCMensagem.AdicionarMensagem(objBEMensagem)
    objDAForum.AddTopico(objBETopico)
  End Sub
  Public Shared Function AdicionarPost(ByVal objBEPost As BEPost, ByVal objBEMensagem As BEMensagem) As Integer
    Dim objDAForum As New DAForum
    objBEPost.IdMensagem = BCMensagem.AdicionarMensagem(objBEMensagem)
    Return objDAForum.Addpost(objBEPost)
  End Function

  Public Shared Function ContaPostsUsuario(ByVal usuario As String) As Integer
    Dim objDAForum As New DAForum
    Return objDAForum.ContaPostsUsuario(usuario)
  End Function


  Public Shared Sub AlteraCategoria(ByVal objBEForum As BEForum)
    Dim objDAForum As New DAForum
    objDAForum.AlteraCategoria(objBEForum)
  End Sub
  Public Shared Sub AlteraSubcategoria(ByVal objBESubcategoria As BESubcategoria)
    Dim objDAForum As New DAForum
    objDAForum.AlteraSubcategoria(objBESubcategoria)
  End Sub
  Public Shared Sub AlteraTopico(ByVal objBETopico As BETopico)
    Dim objDAForum As New DAForum
    objDAForum.AlteraTopico(objBETopico)
  End Sub
  Public Shared Sub AlteraPost(ByVal objBEPost As BEPost)

  End Sub

  Public Shared Sub AlteraVisibilidadeTopico(ByVal objBETopico As BETopico)
    Dim objDAForum As New DAForum
    objDAForum.AlteraVisibilidadeTopico(objBETopico)
  End Sub



  Public Shared Sub ExcluiCategoria(ByVal idCategoria As Integer, ByVal path As String)
    Dim objDAForum As New DAForum
    Dim lstobjBESubcategoria As New List(Of BESubcategoria)
    Dim i As Integer

    lstobjBESubcategoria = objDAForum.SelecionaTodasSubcat(idCategoria)
    For i = 0 To lstobjBESubcategoria.Count - 1
      ExcluiSubcategoria(lstobjBESubcategoria(i).Id, path)
    Next
    objDAForum.ExcluiCategoria(idCategoria)
  End Sub
  Public Shared Sub ExcluiSubcategoria(ByVal idSubcategoria As Integer, ByVal path As String)
    Dim objDAForum As New DAForum
    Dim lstobjBETopico As New List(Of BETopico)
    Dim i As Integer

    lstobjBETopico = objDAForum.SelecionaTodosTopicos(idSubcategoria)
    For i = 0 To lstobjBETopico.Count - 1
      ExcluiTopico(lstobjBETopico(i).Id, path)
    Next
    objDAForum.ExcluiSubcategoria(idSubcategoria)
  End Sub
  Public Shared Sub ExcluiTopico(ByVal idTopico As Integer, ByVal path As String)
    Dim objDAForum As New DAForum
    Dim lstobjBEPost As New List(Of BEPost)
    Dim i As Integer
    lstobjBEPost = objDAForum.SelecionarTodosPosts(idTopico)
    For i = 0 To lstobjBEPost.Count - 1
      ExcluiPost(lstobjBEPost(i).Id, path)
    Next
    objDAForum.ExcluiTopico(idTopico)
    ExcluirImagensForum(1, idTopico, path)
  End Sub
  Public Shared Sub ExcluiPost(ByVal idPost As Integer, ByVal path As String)
    Dim objDAForum As New DAForum
    Dim objDAMensagem As New DAMensagem
    Dim objBEPost As New BEPost
    Dim objBEMensagem As New BEMensagem

    objBEPost = objDAForum.BuscaPost(idPost)
    objBEMensagem = objDAMensagem.BuscaMensagem(objBEPost.IdMensagem)

    objDAForum.ExcluiPost(idPost)
    objDAMensagem.ExcluirMensagem(objBEMensagem)
    ExcluirImagensForum(0, idPost, path)
  End Sub

  Shared Sub ExcluirImagensForum(ByVal tipo As Integer, ByVal idPost As Integer, ByVal caminhoLocal As String)
    Dim bool As Boolean = True
    Dim i As Integer = 0
    Dim strTipo As String

    If tipo = 0 Then
      strTipo = "p"
    Else
      strTipo = "t"
    End If

    While (bool)
      Dim files As String()
      Dim File As String

      files = Directory.GetFiles(caminhoLocal, strTipo & idPost.ToString & "_" & i.ToString & ".*")
      If (Not (files Is Nothing)) And (files.Length > 0) Then
        For Each File In files
          Dim arq As New System.IO.FileInfo(File)
          If arq.Exists Then
            arq.Delete()
          End If
          i = i + 1
        Next
      Else
        bool = False
      End If
    End While

  End Sub

  Public Shared Sub IncVisualizacoes(ByVal idTopico As String)
    Dim objDAForum As New DAForum
    objDAForum.IncVisualizacoes(idTopico)
  End Sub

  Public Shared Function BuscarNoForumSimples(ByVal strFiltro As String) As List(Of BEPesquisaMsg)
    Dim objDAMensagem As New DAForum
    Return objDAMensagem.PesquisarMensagemSimples(strFiltro)
  End Function

  Public Shared Function BuscarNoForumAvancado(ByVal txtBuscaExpressao As String, ByVal txtBuscaQualquer As String, ByVal txtBuscaSem As String, ByVal txtBuscaTodas As String) As List(Of BEPesquisaMsg)
    Dim objDAMensagem As New DAForum
    Return objDAMensagem.PesquisarMensagemAvancada(txtBuscaExpressao, txtBuscaQualquer, txtBuscaSem, txtBuscaTodas)
  End Function

End Class
